package com.anhry.app.safety.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

/**
 * 2008-04-08
 * remark: 在线用户统计
 * author: anhry 
 */
public class OnlineUsers implements HttpSessionListener, HttpSessionAttributeListener,HttpSessionBindingListener {

	private Logger logger = Logger.getLogger(OnlineUsers.class);
	
	private static Map<String, OnlineUser> users = new HashMap<String, OnlineUser>();

	private static OnlineUsers onlineUsers = new OnlineUsers();

	public static OnlineUsers getInstance() {
		return onlineUsers;
	}
	
	/**
	 * @return
	 */
	public Collection getUsers() {
		System.out.println("==============" +users.values().size());
		return users.values();
	}

	public void addUser(OnlineUser user) {
		users.put(user.getId(), user);
		
		logger.info("Add ---> Currnet total online users  is : " + users.size() + "位!");
	}

	public void removeUser(String userId) {
		users.remove(userId);
		logger.info("Remove ---> Currnet total online users  is : " + users.size() + "位!");
	}

	/**
	 * 对象实例(即OnlineUserListener的实例)作为一个属性被设置到session的
	 * 时候，会调用本方法，这种情况一般发生在点击登录按钮以后的处理过程中 设置
	 * 
	 * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
	 */
	public void valueBound(HttpSessionBindingEvent event) {
		// 现在暂时不需要额外处理，你可以在这里记录日志等
	}

	/**
	 * 当Session超时，或本实例被从session中移除的时候被调用，这种情况一般 发生在注销方法的处理过程中
	 * 
	 * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
	 */
	public void valueUnbound(HttpSessionBindingEvent event) {
		try {
			HttpSession session = event.getSession();
			OnlineUser u = (OnlineUser) session.getAttribute("currentUser");
//			System.out.println("删除用户:"+u.getName());
			this.removeUser(u.getId());
			logger.info("Unbound ----> Currnet total online users  is : " + users.size() + "位!");
		} catch (RuntimeException e) {
			 e.printStackTrace();
//			throw e;
		}
	}

	public void sessionCreated(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void sessionDestroyed(HttpSessionEvent event) {
		try {
			HttpSession session = event.getSession();
			OnlineUser u = (OnlineUser) session.getAttribute("currentUser");
//			System.out.println("删除用户:"+u.getName());
			this.removeUser(u.getId());
			logger.info("Unbound ----> Currnet total online users  is : " + users.size() + "位!");
		} catch (RuntimeException e) {
			 e.printStackTrace();
//			throw e;
		}
		
	}

	public void attributeAdded(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
